package cn.brilliantfuture.commentry.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;


/**
 * 
 * Title: Deliver Store
 * 
 * Author: LiTian
 * 
 * Date: 2013-3-26
 * 
 * Description:
 * 
 */
public class RESTUtil {

	private final static String REST_IP = "http://127.0.0.1";
	private final static String APP_KEY = "huoba365";
	private final static String APP_CODE = "TkmSV7VvFR";

	/**
	 * 预定回调方法
	 */
	public static final String STORE_RESERVE_CALLBACK_METHOD = "store.reserve.callback";
	
	/**
	 * 收储到货
	 */
	public static final String DELIVER_INFO_CALLBACK_METHOD = "deliver.info.callback";
	
	/**
	 * 收储取货
	 */
	public static final String TAKEOUT_INFO_CALLBACK_METHOD = "takeout.info.callback";


	public static String getRESTInvoke(String method, String field) {
		StringBuffer sb = new StringBuffer();
		StringBuffer param = new StringBuffer();

		sb.append(REST_IP).append("/dsentry/rest/1.0/callback/service?").append("app_key=").append(APP_KEY).append("&app_code=").append(APP_CODE);

		String timestamp = DateTimeHelper.dateTimeToStr(new Date(), DateTimeHelper.DEFAULT_DATE_TIME_FORMAT);

		param.append("app_key=").append(APP_KEY).append("&app_code=").append(APP_CODE).append("&v=1.0&timestamp=").append(timestamp).append("&format=json&method=").append(method).append("&field=").append(null == field ? "" : field);

		String sign = MD5.getMD5(param.toString());

		try {
			timestamp = URLEncoder.encode(timestamp, "utf-8");
			if (null != field){
				field = URLEncoder.encode(field, "utf-8");
			}
		} catch (UnsupportedEncodingException e) {
			return null;
		}

		sb.append("&v=1.0&timestamp=").append(timestamp).append("&format=json&method=").append(method).append("&field=").append(null == field ? "" : field).append("&sign=").append(sign);

		return HttpUtil.getInvoke(sb.toString());
	}
}
